import 'package:app_core/app_core.dart';
import 'package:flutter/material.dart';
import 'package:yh_core_flutter_project/project/common/constant/colors.dart';
import 'package:yh_core_flutter_project/project/common/constant/styles.dart';

/// YHSmartDialog 功能测试页面
class SmartDialogTestPage extends StatelessWidget {
  const SmartDialogTestPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar('SmartDialogTest测试'),
      backgroundColor: AppColor.background,
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _buildSectionTitle('基础对话框'),
            _buildTestButton('确认对话框', () => _testConfirmDialog()),
            _buildTestButton('警告对话框', () => _testWarningDialog()),
            _buildTestButton('危险操作确认', () => _testDangerConfirm()),
            _buildTestButton('信息对话框', () => _testInfoDialog()),
            _buildTestButton('自定义对话框', () => _testCustomDialog()),

            const SizedBox(height: 24),
            _buildSectionTitle('消息提示'),
            ElevatedButton(
              onPressed: () => YHSmartDialog.showMsg('这是顶部消息', alignment: Alignment.topCenter),
              child: const Text('普通消息（上）'),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () => YHSmartDialog.showMsg('这是底部消息', alignment: Alignment.bottomCenter),
              child: const Text('普通消息（下）'),
            ),
            _buildTestButton('普通消息', () => _testNormalMsg()),
            _buildTestButton('成功消息', () => _testSuccessMsg()),
            _buildTestButton('警告消息', () => _testWarningMsg()),
            _buildTestButton('错误消息', () => _testErrorMsg()),

            const SizedBox(height: 24),
            _buildSectionTitle('Notify 通知条'),
            _buildTestButton('测试配置的成功通知', () => _testConfiguredSuccessNotify()),
            _buildTestButton('测试配置的错误通知', () => _testConfiguredErrorNotify()),
            _buildTestButton('测试配置的警告通知', () => _testConfiguredWarningNotify()),
            _buildTestButton('测试配置的提醒通知', () => _testConfiguredAlertNotify()),
            _buildTestButton('测试配置的失败通知', () => _testConfiguredFailureNotify()),

            const SizedBox(height: 24),
            _buildSectionTitle('Loading 加载'),
            _buildTestButton('显示 Loading', () => _testShowLoading()),
            _buildTestButton('显示 Loading (3秒后自动关闭)', () => _testShowLoadingWithAutoClose()),
            _buildTestButton('关闭 Loading', () => _testCloseLoading()),

          ],
        ),
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    return Padding(padding: const EdgeInsets.symmetric(vertical: 8), child: Text(title, style: AppStyles.s18b.copyWith(color: AppColor.primary)));
  }

  Widget _buildTestButton(String text, VoidCallback onPressed) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: AppColor.buttonPrimary,
          foregroundColor: AppColor.buttonText,
          padding: const EdgeInsets.symmetric(vertical: 12),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
        child: Text(text, style: AppStyles.s16w5.copyWith(color: AppColor.buttonText)),
      ),
    );
  }

  // ===== 基础对话框测试 =====
  void _testConfirmDialog() async {
    final result = await YHSmartDialog.showConfirm(title: '确认操作', content: '您确定要执行此操作吗？此操作不可撤销。', confirmText: '确定', cancelText: '取消');

    if (result == true) {
      YHSmartDialog.showMsg('用户点击了确定');
    } else if (result == false) {
      YHSmartDialog.showMsg('用户点击了取消');
    }
  }

  void _testWarningDialog() async {
    final result = await YHSmartDialog.showConfirm(title: '警告提示', content: '您的操作可能存在风险，请谨慎处理。', confirmText: '我知道了', showCancel: false);

    if (result == true) {
      YHSmartDialog.showMsg('用户确认了警告');
    }
  }

  void _testDangerConfirm() async {
    final result = await YHSmartDialog.showDangerConfirm(title: '危险操作', content: '这是一个危险操作，执行后将无法撤销，请确认是否继续？', confirmText: '确认删除', cancelText: '取消');

    if (result == true) {
      YHSmartDialog.showMsg('危险操作已执行');
    } else if (result == false) {
      YHSmartDialog.showMsg('用户取消了危险操作');
    }
  }

  void _testInfoDialog() async {
    final result = await YHSmartDialog.showInfo(title: '信息提示', content: '这是一条重要的信息，请仔细阅读。', confirmText: '我知道了', barrierDismissible: false);

    if (result == true) {
      YHSmartDialog.showMsg('用户确认了信息');
    }
  }

  void _testCustomDialog() async {
    final result = await YHSmartDialog.showConfirm(
      title: '自定义对话框',
      content: '这是一个自定义样式的对话框，支持各种配置选项。',
      confirmText: '确认',
      cancelText: '取消',
      confirmColor: AppColor.success,
      showCancel: true,
      barrierDismissible: false,
    );

    if (result == true) {
      YHSmartDialog.showMsg('用户确认了自定义对话框');
    } else if (result == false) {
      YHSmartDialog.showMsg('用户取消了自定义对话框');
    }
  }

  // ===== 消息提示测试 =====

  void _testNormalMsg() {
    YHSmartDialog.showMsg('这是一个普通的提示信息');
  }

  void _testSuccessMsg() {
    YHSmartDialog.showSuccessMsg('操作成功完成');
  }

  void _testWarningMsg() {
    YHSmartDialog.showWarningMsg('请注意警告信息');
  }

  void _testErrorMsg() {
    YHSmartDialog.showErrorMsg('发生了错误');
  }

  // ===== Loading 测试 =====
  void _testShowLoading() {
    YHSmartDialog.showLoading(message: '正在加载...');
  }

  void _testShowLoadingWithAutoClose() {
    YHSmartDialog.showLoading(message: '3秒后自动关闭...');

    Future.delayed(const Duration(seconds: 3), () {
      YHSmartDialog.dismiss();
      YHSmartDialog.showMsg('Loading 已自动关闭');
    });
  }

  void _testCloseLoading() {
    YHSmartDialog.dismiss();
    YHSmartDialog.showMsg('Loading 已手动关闭');
  }

  /// 测试配置的成功通知
  void _testConfiguredSuccessNotify() {
    YHSmartDialog.showTopSuccessNotify('操作成功完成！');
  }

  /// 测试配置的错误通知
  void _testConfiguredErrorNotify() {
    YHSmartDialog.showTopErrorNotify('操作失败，请重试。');
  }

  /// 测试配置的警告通知
  void _testConfiguredWarningNotify() {
    YHSmartDialog.showTopWarningNotify('您的操作可能存在风险，请谨慎处理。');
  }

  /// 测试配置的提醒通知
  void _testConfiguredAlertNotify() {
    YHSmartDialog.showTopAlertNotify('这是一个提醒通知。');
  }

  /// 测试配置的失败通知
  void _testConfiguredFailureNotify() {
    YHSmartDialog.showTopFailureNotify('操作失败。');
  }

}
